package com.margo.project.web.session.configuration;

import java.nio.charset.Charset;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
//import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;

/**
 * RedisTemplate extends RedisAccessor implements InitializingBean
 * 	重写的 afterPropertiesSet 方法中 判断 defaultSerializer为null,如果为null,使用默认的序列化JdkSerializationRedisSerializer
 * RedisSerializer keySerializer ;
 * RedisSerializer valueSerializer ;
 * RedisSerializer hashKeySerializer ;
 * RedisSerializer hashValueSerializer ;
 * 
 * @author fangxingbang
 *
 */
@Component
public class MargoRedisPostProcessor implements BeanPostProcessor{
	
	/**
	 * 	在afterPropertiesSet 方法调用之前调用
	 */
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if(bean instanceof RedisTemplate<?, ?>) {
			//这里设置redisTemplate 序列化，默认的序列化工具
			RedisTemplate<?, ?> redisTemplate=(RedisTemplate<?, ?>) bean;
			redisTemplate.setDefaultSerializer(new StringRedisSerializer(Charset.forName("UTF-8")));
		}
		return bean;
	}

	
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}
}
